package com.wcsy.myblog.config;


import com.wcsy.myblog.realm.UserRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
     @Bean
     public ShiroFilterFactoryBean shiroFilterFactoryBean(){
         ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
         bean.setSecurityManager(getsecurityManager());
         bean.setLoginUrl("/login"); //拦截后跳转到登陆页面
         bean.setUnauthorizedUrl("/noauth");//设置为未授权的请求跳转的页面
         Map<String,String> map = new LinkedHashMap<>();
         /*     anon：无需认证就可以访问
          *      authc：必须认证了才能访问
          *      user :必须拥有 记住我 功能才能用
          *      perms： 拥有对某个资源的权限才能访问
          *      role：拥有某个角色权限才能访问
          * */
          map.put("/","anon");
          map.put("/index","anon");
          map.put("/user/login","anon");
          map.put("/static/**","anon");
          map.put("/login","anon");
         // map.put("/vip","perms[user:vip]");  //拥有vip权限才能访问
         //   map.put("/admin","perms[user:admin]");  //拥有管理员权限才能访问
        //  map.put("/**","authc");
         bean.setFilterChainDefinitionMap(map);
         return bean;
     }
    @Bean
    public SecurityManager getsecurityManager(){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(getuserRealm());
        return manager;
    }
    @Bean
    public UserRealm getuserRealm(){
        return new UserRealm();
    }

}
